{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 🎆 Convolutions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook, we'll walk through how convolutional filters can pick out different aspects of an image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from skimage import data\n",
    "from skimage.color import rgb2gray\n",
    "from skimage.transform import resize"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 0. Original Input Image <a name=\"orig\"></a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "im = rgb2gray(data.coffee())\n",
    "im = resize(im, (64, 64))\n",
    "print(im.shape)\n",
    "\n",
    "plt.axis(\"off\")\n",
    "plt.imshow(im, cmap=\"gray\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Horizontal Edge Filter <a name=\"hor\"></a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filter1 = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]])\n",
    "\n",
    "new_image = np.zeros(im.shape)\n",
    "\n",
    "im_pad = np.pad(im, 1, \"constant\")\n",
    "\n",
    "for i in range(im.shape[0]):\n",
    "    for j in range(im.shape[1]):\n",
    "        try:\n",
    "            new_image[i, j] = (\n",
    "                im_pad[i - 1, j - 1] * filter1[0, 0]\n",
    "                + im_pad[i - 1, j] * filter1[0, 1]\n",
    "                + im_pad[i - 1, j + 1] * filter1[0, 2]\n",
    "                + im_pad[i, j - 1] * filter1[1, 0]\n",
    "                + im_pad[i, j] * filter1[1, 1]\n",
    "                + im_pad[i, j + 1] * filter1[1, 2]\n",
    "                + im_pad[i + 1, j - 1] * filter1[2, 0]\n",
    "                + im_pad[i + 1, j] * filter1[2, 1]\n",
    "                + im_pad[i + 1, j + 1] * filter1[2, 2]\n",
    "            )\n",
    "        except:\n",
    "            pass\n",
    "\n",
    "plt.axis(\"off\")\n",
    "plt.imshow(new_image, cmap=\"Greys\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Vertical Edge Filter <a name=\"ver\"></a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filter2 = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])\n",
    "\n",
    "new_image = np.zeros(im.shape)\n",
    "\n",
    "im_pad = np.pad(im, 1, \"constant\")\n",
    "\n",
    "for i in range(im.shape[0]):\n",
    "    for j in range(im.shape[1]):\n",
    "        try:\n",
    "            new_image[i, j] = (\n",
    "                im_pad[i - 1, j - 1] * filter2[0, 0]\n",
    "                + im_pad[i - 1, j] * filter2[0, 1]\n",
    "                + im_pad[i - 1, j + 1] * filter2[0, 2]\n",
    "                + im_pad[i, j - 1] * filter2[1, 0]\n",
    "                + im_pad[i, j] * filter2[1, 1]\n",
    "                + im_pad[i, j + 1] * filter2[1, 2]\n",
    "                + im_pad[i + 1, j - 1] * filter2[2, 0]\n",
    "                + im_pad[i + 1, j] * filter2[2, 1]\n",
    "                + im_pad[i + 1, j + 1] * filter2[2, 2]\n",
    "            )\n",
    "        except:\n",
    "            pass\n",
    "\n",
    "plt.axis(\"off\")\n",
    "plt.imshow(new_image, cmap=\"Greys\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Horizontal Edge Filter with Stride 2 <a name=\"hor2\"></a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filter1 = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]])\n",
    "\n",
    "stride = 2\n",
    "\n",
    "new_image = np.zeros((int(im.shape[0] / stride), int(im.shape[1] / stride)))\n",
    "\n",
    "im_pad = np.pad(im, 1, \"constant\")\n",
    "\n",
    "for i in range(0, im.shape[0], stride):\n",
    "    for j in range(0, im.shape[1], stride):\n",
    "        try:\n",
    "            new_image[int(i / stride), int(j / stride)] = (\n",
    "                im_pad[i - 1, j - 1] * filter1[0, 0]\n",
    "                + im_pad[i - 1, j] * filter1[0, 1]\n",
    "                + im_pad[i - 1, j + 1] * filter1[0, 2]\n",
    "                + im_pad[i, j - 1] * filter1[1, 0]\n",
    "                + im_pad[i, j] * filter1[1, 1]\n",
    "                + im_pad[i, j + 1] * filter1[1, 2]\n",
    "                + im_pad[i + 1, j - 1] * filter1[2, 0]\n",
    "                + im_pad[i + 1, j] * filter1[2, 1]\n",
    "                + im_pad[i + 1, j + 1] * filter1[2, 2]\n",
    "            )\n",
    "        except:\n",
    "            pass\n",
    "\n",
    "plt.axis(\"off\")\n",
    "plt.imshow(new_image, cmap=\"Greys\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Vertical Edge Filter with Stride 2 <a name=\"ver2\"></a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filter2 = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])\n",
    "\n",
    "stride = 2\n",
    "\n",
    "new_image = np.zeros((int(im.shape[0] / stride), int(im.shape[1] / stride)))\n",
    "\n",
    "im_pad = np.pad(im, 1, \"constant\")\n",
    "\n",
    "for i in range(0, im.shape[0], stride):\n",
    "    for j in range(0, im.shape[1], stride):\n",
    "        try:\n",
    "            new_image[int(i / stride), int(j / stride)] = (\n",
    "                im_pad[i - 1, j - 1] * filter2[0, 0]\n",
    "                + im_pad[i - 1, j] * filter2[0, 1]\n",
    "                + im_pad[i - 1, j + 1] * filter2[0, 2]\n",
    "                + im_pad[i, j - 1] * filter2[1, 0]\n",
    "                + im_pad[i, j] * filter2[1, 1]\n",
    "                + im_pad[i, j + 1] * filter2[1, 2]\n",
    "                + im_pad[i + 1, j - 1] * filter2[2, 0]\n",
    "                + im_pad[i + 1, j] * filter2[2, 1]\n",
    "                + im_pad[i + 1, j + 1] * filter2[2, 2]\n",
    "            )\n",
    "        except:\n",
    "            pass\n",
    "\n",
    "plt.axis(\"off\")\n",
    "plt.imshow(new_image, cmap=\"Greys\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "vscode": {
   "interpreter": {
    "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
